<!DOCTYPE html>
<html lang=zh>
<head>
    <meta charset="utf-8">
    
    <title>第三十五章：SpringBoot与单元测试的小秘密 | 恒宇少年De成长之路</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="单元测试对于开发人员来说是非常熟悉的，我们每天的工作也都是围绕着开发与测试进行的，在最早的时候测试都是采用工具Debug模式进行调试程序，后来Junit的诞生也让程序测试发生了很大的变化。我们今天来讲解下基于SpringBoot结合Junit怎么来完成单元测试。">
<meta name="keywords" content="SpringBoot">
<meta property="og:type" content="article">
<meta property="og:title" content="第三十五章：SpringBoot与单元测试的小秘密">
<meta property="og:url" content="http://blog.yuqiyu.com/spring-boot-chapter-35/index.html">
<meta property="og:site_name" content="恒宇少年De成长之路">
<meta property="og:description" content="单元测试对于开发人员来说是非常熟悉的，我们每天的工作也都是围绕着开发与测试进行的，在最早的时候测试都是采用工具Debug模式进行调试程序，后来Junit的诞生也让程序测试发生了很大的变化。我们今天来讲解下基于SpringBoot结合Junit怎么来完成单元测试。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/4461954-f1d4cb64234b160a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-01-23T15:30:42.828Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="第三十五章：SpringBoot与单元测试的小秘密">
<meta name="twitter:description" content="单元测试对于开发人员来说是非常熟悉的，我们每天的工作也都是围绕着开发与测试进行的，在最早的时候测试都是采用工具Debug模式进行调试程序，后来Junit的诞生也让程序测试发生了很大的变化。我们今天来讲解下基于SpringBoot结合Junit怎么来完成单元测试。">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/4461954-f1d4cb64234b160a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
    

    
        <link rel="alternate" href="/" title="恒宇少年De成长之路" type="application/atom+xml" />
    

    

    <link rel="stylesheet" href="/libs/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/libs/open-sans/styles.css">
    <link rel="stylesheet" href="/libs/source-code-pro/styles.css">

    <link rel="stylesheet" href="/css/style.css">

    <script src="/libs/jquery/2.1.3/jquery.min.js"></script>
    
    
        <link rel="stylesheet" href="/libs/lightgallery/css/lightgallery.min.css">
    
    
        <link rel="stylesheet" href="/libs/justified-gallery/justifiedGallery.min.css">
    
    
    
    


</head>

<body>
    <div id="container">
        <header id="header">
    <div id="header-main" class="header-inner">
        <div class="outer">
            <a href="/" id="logo">
                <i class="logo"></i>
                <span class="site-title">恒宇少年De成长之路</span>
            </a>
            <nav id="main-nav">
                
                    <a class="main-nav-link" href="/.">首页</a>
                
                    <a class="main-nav-link" href="/archives">时间轴</a>
                
                    <a class="main-nav-link" href="/categories">文章专题</a>
                
                    <a class="main-nav-link" href="/tags">标签云</a>
                
                    <a class="main-nav-link" href="/about">关于我</a>
                
            </nav>
            
                
                <nav id="sub-nav">
                    <div class="profile" id="profile-nav">
                        <a id="profile-anchor" href="javascript:;">
                            <img class="avatar" src="/css/images/avatar.png" />
                            <i class="fa fa-caret-down"></i>
                        </a>
                    </div>
                </nav>
            
            <div id="search-form-wrap">

    <form class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="搜索" />
        <!--button type="submit" class="search-form-submit"></button-->
    </form>
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="想要查找什么..." />
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>

</div>
        </div>
    </div>
    <div id="main-nav-mobile" class="header-sub header-inner">
        <table class="menu outer">
            <tr>
                
                    <td ><a  class="main-nav-link" href="/.">首页</a></td>
                
                    <td ><a  class="main-nav-link" href="/archives">时间轴</a></td>
                
                    <td ><a  class="main-nav-link" href="/categories">文章专题</a></td>
                
                    <td ><a  class="main-nav-link" href="/tags">标签云</a></td>
                
                    <td ><a  class="main-nav-link" href="/about">关于我</a></td>
                
                <td>
                    
    <div class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="搜索" />
    </div>

                </td>
            </tr>
        </table>
    </div>
</header>

        <div class="outer">
            
                

<aside id="profile">
    <div class="inner profile-inner">
        <div class="base-info profile-block">
            <img id="avatar" src="/css/images/avatar.png" />
            <h2 id="name">恒宇少年</h2>
            <h3 id="title">Java软件工程师 &amp; 程序猿</h3>
            <span id="location"><i class="fa fa-map-marker"></i>山东, 济南</span>
            <a id="follow" target="_blank" href="https://www.jianshu.com/u/092df3f77bca">关注我</a>
        </div>
        <div class="article-info profile-block">
            <div class="article-info-block">
                43
                <span>文章</span>
            </div>
            <div class="article-info-block">
                2
                <span>标签</span>
            </div>
        </div>
        
        <div class="profile-block social-links">
            <table>
                <tr>
                    
                    
                    <td>
                        <a href="http://github.com/ppoffice/hexo-theme-icarus" target="_blank" title="github" class=tooltip>
                            <i class="fa fa-github"></i>
                        </a>
                    </td>
                    
                    <td>
                        <a href="/" target="_blank" title="twitter" class=tooltip>
                            <i class="fa fa-twitter"></i>
                        </a>
                    </td>
                    
                    <td>
                        <a href="/" target="_blank" title="facebook" class=tooltip>
                            <i class="fa fa-facebook"></i>
                        </a>
                    </td>
                    
                    <td>
                        <a href="/" target="_blank" title="dribbble" class=tooltip>
                            <i class="fa fa-dribbble"></i>
                        </a>
                    </td>
                    
                    <td>
                        <a href="/" target="_blank" title="rss" class=tooltip>
                            <i class="fa fa-rss"></i>
                        </a>
                    </td>
                    
                </tr>
            </table>
        </div>
        
    </div>
</aside>

            
            <section id="main"><article id="post-spring-boot-chapter-35" class="article article-type-post" itemscope itemprop="blogPost">
    <div class="article-inner">
        
        
            <header class="article-header">
                
    
        <h1 class="article-title" itemprop="name">
            第三十五章：SpringBoot与单元测试的小秘密
        </h1>
    

                
                    <div class="article-meta">
                        
    <div class="article-date">
        <i class="fa fa-calendar"></i>
        <a href="/spring-boot-chapter-35/">
            <time datetime="2017-09-13T16:00:00.000Z" itemprop="datePublished">2017-09-14</time>
        </a>
    </div>


                        
    <div class="article-category">
    	<i class="fa fa-folder"></i>
        <a class="article-category-link" href="/categories/SpringBoot-核心技术/">SpringBoot 核心技术</a>
    </div>

                        
    <div class="article-tag">
        <i class="fa fa-tag"></i>
        <a class="tag-link" href="/tags/SpringBoot/">SpringBoot</a>
    </div>

                    </div>
                
            </header>
        
        
        <div class="article-entry" itemprop="articleBody">
        
            
            <p><code>单元测试</code>对于开发人员来说是非常熟悉的，我们每天的工作也都是围绕着开发与测试进行的，在最早的时候测试都是采用工具<code>Debug</code>模式进行调试程序，后来<code>Junit</code>的诞生也让程序测试发生了很大的变化。我们今天来讲解下基于<code>SpringBoot</code>结合<code>Junit</code>怎么来完成<code>单元测试</code>。<br><a id="more"></a></p>
<p>#本章目的<br>基于<code>SpringBoot</code>平台整合<code>Junit</code>分别完成<code>客户端</code>、<code>服务端</code>的<code>单元测试</code>。</p>
<h1 id="SpringBoot-企业级核心技术学习专题"><a href="#SpringBoot-企业级核心技术学习专题" class="headerlink" title="SpringBoot 企业级核心技术学习专题"></a>SpringBoot 企业级核心技术学习专题</h1><table>
<thead>
<tr>
<th style="text-align:center">专题</th>
<th style="text-align:left">专题名称</th>
<th style="text-align:left">专题描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">001</td>
<td style="text-align:left"><a href="http://www.jianshu.com/c/3f69deddbed3" target="_blank" rel="noopener">Spring Boot 核心技术</a></td>
<td style="text-align:left">讲解SpringBoot一些企业级层面的核心组件</td>
</tr>
<tr>
<td style="text-align:center">002</td>
<td style="text-align:left"><a href="https://gitee.com/hengboy/spring-boot-chapter" target="_blank" rel="noopener">Spring Boot 核心技术章节源码</a></td>
<td style="text-align:left">Spring Boot 核心技术简书每一篇文章码云对应源码</td>
</tr>
<tr>
<td style="text-align:center">003</td>
<td style="text-align:left"><a href="http://www.jianshu.com/c/1faac22666e7" target="_blank" rel="noopener">Spring Cloud 核心技术</a></td>
<td style="text-align:left">对Spring Cloud核心技术全面讲解</td>
</tr>
<tr>
<td style="text-align:center">004</td>
<td style="text-align:left"><a href="https://gitee.com/hengboy/spring-cloud-chapter" target="_blank" rel="noopener">Spring Cloud 核心技术章节源码</a></td>
<td style="text-align:left">Spring Cloud 核心技术简书每一篇文章对应源码</td>
</tr>
<tr>
<td style="text-align:center">005</td>
<td style="text-align:left"><a href="http://www.jianshu.com/c/ab4789177827" target="_blank" rel="noopener">QueryDSL 核心技术</a></td>
<td style="text-align:left">全面讲解QueryDSL核心技术以及基于SpringBoot整合SpringDataJPA</td>
</tr>
<tr>
<td style="text-align:center">006</td>
<td style="text-align:left"><a href="http://www.jianshu.com/c/f1b269bb2fd6" target="_blank" rel="noopener">SpringDataJPA 核心技术</a></td>
<td style="text-align:left">全面讲解SpringDataJPA核心技术</td>
</tr>
</tbody>
</table>
<h1 id="构建项目"><a href="#构建项目" class="headerlink" title="构建项目"></a>构建项目</h1><p>我们首先使用idea工具创建一个<code>SpringBoot</code>项目，并且添加相关Web、MySQL、JPA依赖，具体pom.xml配置依赖内容如下所示：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">.../省略其他配置</span><br><span class="line">&lt;dependencies&gt;</span><br><span class="line">		&lt;!--web依赖--&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line">		&lt;!--data jpa依赖--&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line">		&lt;!--druid数据源依赖--&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;com.alibaba&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;druid&lt;/artifactId&gt;</span><br><span class="line">			&lt;version&gt;1.0.31&lt;/version&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line">		&lt;!--lombok依赖--&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;lombok&lt;/artifactId&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line">		&lt;!--MySQL依赖--&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;mysql&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line"></span><br><span class="line">		&lt;!--springboot程序测试依赖，创建项目默认添加--&gt;</span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;</span><br><span class="line">			&lt;scope&gt;test&lt;/scope&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line"></span><br><span class="line">	&lt;/dependencies&gt;</span><br><span class="line">.../省略其他配置</span><br></pre></td></tr></table></figure></p>
<p>##配置数据库<br>我们本章的内容需要访问数据库，我们先在<code>src/main/resources</code>下添加<code>application.yml</code>配置文件，对应添加数据库配置信息如下所示：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">spring:</span><br><span class="line">  datasource:</span><br><span class="line">    type: com.alibaba.druid.pool.DruidDataSource</span><br><span class="line">    driver-class-name: com.mysql.jdbc.Driver</span><br><span class="line">    url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8</span><br><span class="line">    username: root</span><br><span class="line">    password: 123456</span><br><span class="line">    #最大活跃数</span><br><span class="line">    maxActive: 20</span><br><span class="line">    #初始化数量</span><br><span class="line">    initialSize: 1</span><br><span class="line">    #最大连接等待超时时间</span><br><span class="line">    maxWait: 60000</span><br><span class="line">    #打开PSCache，并且指定每个连接PSCache的大小</span><br><span class="line">    poolPreparedStatements: true</span><br><span class="line">    maxPoolPreparedStatementPerConnectionSize: 20</span><br><span class="line">    #通过connectionProperties属性来打开mergeSql功能；慢SQL记录</span><br><span class="line">    #connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000</span><br><span class="line">    minIdle: 1</span><br><span class="line">    timeBetweenEvictionRunsMillis: 60000</span><br><span class="line">    minEvictableIdleTimeMillis: 300000</span><br><span class="line">    validationQuery: select 1 from dual</span><br><span class="line">    testWhileIdle: true</span><br><span class="line">    testOnBorrow: false</span><br><span class="line">    testOnReturn: false</span><br><span class="line">    #配置监控统计拦截的filters，去掉后监控界面sql将无法统计,&apos;wall&apos;用于防火墙</span><br><span class="line">    filters: stat, wall, log4j</span><br><span class="line">  jpa:</span><br><span class="line">    properties:</span><br><span class="line">      hibernate:</span><br><span class="line">        show_sql: true</span><br><span class="line">        format_sql: true</span><br></pre></td></tr></table></figure></p>
<p>以上配置都是比较常用到，这里不做多解释了，如果不明白可以去本文底部<code>SpringBoot学习目录</code>文章内找寻对应的章节。</p>
<p>##构建实体<br>对应数据库内的数据表来创建一个商品基本信息实体，实体内容如下所示：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">package com.yuqiyu.chapter35.bean;</span><br><span class="line"></span><br><span class="line">import lombok.Data;</span><br><span class="line"></span><br><span class="line">import javax.persistence.*;</span><br><span class="line">import java.io.Serializable;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 商品基本信息实体</span><br><span class="line"> * ========================</span><br><span class="line"> * Created with IntelliJ IDEA.</span><br><span class="line"> * User：恒宇少年</span><br><span class="line"> * Date：2017/9/13</span><br><span class="line"> * Time：22:20</span><br><span class="line"> * 码云：http://git.oschina.net/jnyqy</span><br><span class="line"> * ========================</span><br><span class="line"> */</span><br><span class="line">@Data</span><br><span class="line">@Entity</span><br><span class="line">@Table(name = &quot;good_infos&quot;)</span><br><span class="line">public class GoodInfoEntity implements Serializable</span><br><span class="line">&#123;</span><br><span class="line">    //商品编号</span><br><span class="line">    @Id</span><br><span class="line">    @Column(name = &quot;tg_id&quot;)</span><br><span class="line">    @GeneratedValue</span><br><span class="line">    private Integer tgId;</span><br><span class="line"></span><br><span class="line">    //商品类型编号</span><br><span class="line">    @Column(name = &quot;tg_type_id&quot;)</span><br><span class="line">    private Integer typeId;</span><br><span class="line"></span><br><span class="line">    //商品标题</span><br><span class="line">    @Column(name = &quot;tg_title&quot;)</span><br><span class="line">    private String title;</span><br><span class="line"></span><br><span class="line">    //商品价格</span><br><span class="line">    @Column(name = &quot;tg_price&quot;)</span><br><span class="line">    private double price;</span><br><span class="line"></span><br><span class="line">    //商品排序</span><br><span class="line">    @Column(name = &quot;tg_order&quot;)</span><br><span class="line">    private int order;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>##构建JPA<br>基于商品基本信息实体类创建一个JPA接口，该接口继承<code>JpaRepository</code>接口完成框架通过反向代理模式进行生成实现类，自定义JPA接口内容如下所示：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">package com.yuqiyu.chapter35.jpa;</span><br><span class="line"></span><br><span class="line">import com.yuqiyu.chapter35.bean.GoodInfoEntity;</span><br><span class="line">import org.springframework.data.jpa.repository.JpaRepository;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 商品jpa</span><br><span class="line"> * ========================</span><br><span class="line"> * Created with IntelliJ IDEA.</span><br><span class="line"> * User：恒宇少年</span><br><span class="line"> * Date：2017/9/13</span><br><span class="line"> * Time：22:23</span><br><span class="line"> * 码云：http://git.oschina.net/jnyqy</span><br><span class="line"> * ========================</span><br><span class="line"> */</span><br><span class="line">public interface GoodInfoJPA</span><br><span class="line">    extends JpaRepository&lt;GoodInfoEntity,Integer&gt;</span><br><span class="line">&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>##构建测试控制器<br>下面我们开始为<code>单元测试</code>来做准备工作，先来创建一个<code>SpringMVC</code>控制器来处理请求，代码如下所示：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line">package com.yuqiyu.chapter35.controller;</span><br><span class="line"></span><br><span class="line">import com.yuqiyu.chapter35.bean.GoodInfoEntity;</span><br><span class="line">import com.yuqiyu.chapter35.jpa.GoodInfoJPA;</span><br><span class="line">import org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line">import org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line">import org.springframework.web.bind.annotation.RequestMethod;</span><br><span class="line">import org.springframework.web.bind.annotation.RestController;</span><br><span class="line"></span><br><span class="line">import java.util.List;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * ===============================</span><br><span class="line"> * Created with Eclipse.</span><br><span class="line"> * User：于起宇</span><br><span class="line"> * Date：2017/9/13</span><br><span class="line"> * Time：18:37</span><br><span class="line"> * 简书：http://www.jianshu.com/u/092df3f77bca</span><br><span class="line"> * ================================</span><br><span class="line"> */</span><br><span class="line">@RestController</span><br><span class="line">public class TestController</span><br><span class="line">&#123;</span><br><span class="line">    //商品基本信息数据接口</span><br><span class="line">    @Autowired</span><br><span class="line">    private GoodInfoJPA goodInfoJPA;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 查询首页内容</span><br><span class="line">     * @return</span><br><span class="line">     */</span><br><span class="line">    @RequestMapping(value = &quot;/index&quot;)</span><br><span class="line">    public String index(String name)</span><br><span class="line">    &#123;</span><br><span class="line">        return &quot;this is index page&quot; + name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 查询全部商品</span><br><span class="line">     * @return</span><br><span class="line">     */</span><br><span class="line">    @RequestMapping(value = &quot;/all&quot;)</span><br><span class="line">    public List&lt;GoodInfoEntity&gt; selectAll()</span><br><span class="line">    &#123;</span><br><span class="line">        return goodInfoJPA.findAll();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 查询商品详情</span><br><span class="line">     * @param goodId</span><br><span class="line">     * @return</span><br><span class="line">     */</span><br><span class="line">    @RequestMapping(value = &quot;/detail&quot;,method = RequestMethod.GET)</span><br><span class="line">    public GoodInfoEntity selectOne(Integer goodId)</span><br><span class="line">    &#123;</span><br><span class="line">        return goodInfoJPA.findOne(goodId);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>我们在测试控制内注入了<code>GoodInfoJPA</code>，获得了操作商品基本信息的数据接口代理实例，我们可以通过该代理实例去做一些数据库操作，如上代码<code>selectAll</code>、<code>detail</code>方法所示。<br>在测试控制器内添加了三个测试<code>MVC</code>方法，我们接下来开始编写<code>单元测试</code>代码。</p>
<p>#编写单元测试</p>
<p>在我们使用idea开发工具构建完成<code>SpringBoot</code>项目后，会自动为我们添加<code>spring-boot-starter-test</code>依赖到<code>pom.xml</code>配置文件内，当然也为我们自动创建了一个测试类，该类内一开始是没有过多的代码的。<br>下面我们开始基于该测试类进行添加逻辑，代码如下所示：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">....//省略依赖导包</span><br><span class="line">/**</span><br><span class="line"> * 单元测试</span><br><span class="line"> */</span><br><span class="line">@RunWith(SpringRunner.class)</span><br><span class="line">@SpringBootTest</span><br><span class="line">public class Chapter35ApplicationTests &#123;</span><br><span class="line">	/**</span><br><span class="line">	 * 模拟mvc测试对象</span><br><span class="line">	 */</span><br><span class="line">	private MockMvc mockMvc;</span><br><span class="line"></span><br><span class="line">	/**</span><br><span class="line">	 * web项目上下文</span><br><span class="line">	 */</span><br><span class="line">	@Autowired</span><br><span class="line">	private WebApplicationContext webApplicationContext;</span><br><span class="line"></span><br><span class="line">	/**</span><br><span class="line">	 * 商品业务数据接口</span><br><span class="line">	 */</span><br><span class="line">	@Autowired</span><br><span class="line">	private GoodInfoJPA goodInfoJPA;</span><br><span class="line"></span><br><span class="line">	/**</span><br><span class="line">	 * 所有测试方法执行之前执行该方法</span><br><span class="line">	 */</span><br><span class="line">	@Before</span><br><span class="line">	public void before() &#123;</span><br><span class="line">		//获取mockmvc对象实例</span><br><span class="line">		mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>在上面测试代码中我们从上面开始讲解下，其中<code>@RunWith</code>这里就不多做解释了，我们最比较常用到的就是这个注解。</p>
<p><code>@SpringBootTest</code>这个注解这里要强调下，这是<code>SpringBoot</code>项目测试的核心注解，标识该测试类以<code>SpringBoot</code>方式运行，该注解的源码如下所示：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">...//省略导包</span><br><span class="line">@Target(&#123;ElementType.TYPE&#125;)</span><br><span class="line">@Retention(RetentionPolicy.RUNTIME)</span><br><span class="line">@Documented</span><br><span class="line">@Inherited</span><br><span class="line">@BootstrapWith(SpringBootTestContextBootstrapper.class)</span><br><span class="line">public @interface SpringBootTest &#123;</span><br><span class="line">    @AliasFor(&quot;properties&quot;)</span><br><span class="line">    String[] value() default &#123;&#125;;</span><br><span class="line"></span><br><span class="line">    @AliasFor(&quot;value&quot;)</span><br><span class="line">    String[] properties() default &#123;&#125;;</span><br><span class="line"></span><br><span class="line">    Class&lt;?&gt;[] classes() default &#123;&#125;;</span><br><span class="line"></span><br><span class="line">    SpringBootTest.WebEnvironment webEnvironment() default SpringBootTest.WebEnvironment.MOCK;</span><br><span class="line"></span><br><span class="line">    public static enum WebEnvironment &#123;</span><br><span class="line">        MOCK(false),</span><br><span class="line">        RANDOM_PORT(true),</span><br><span class="line">        DEFINED_PORT(true),</span><br><span class="line">        NONE(false);</span><br><span class="line"></span><br><span class="line">        private final boolean embedded;</span><br><span class="line"></span><br><span class="line">        private WebEnvironment(boolean embedded) &#123;</span><br><span class="line">            this.embedded = embedded;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        public boolean isEmbedded() &#123;</span><br><span class="line">            return this.embedded;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>我们可以看到在<code>@SpringBootTest</code>注解源码中最为重要的就是<code>@BootstrapWith</code>，该注解才是配置了测试类的启动方式，以及启动时使用实现类的类型。</p>
<p>###测试index请求<br><code>MockMvc</code>这个类是一个被<code>final</code>修饰的类型，该类无法被继承使用。这个类是<code>Spring</code>为我们提供模拟<code>SpringMVC</code>请求的实例类，该类则是由<code>MockMvcBuilders</code>通过<code>WebApplicationContext</code>实例进行创建的，初始化<code>MockMvc</code>实例我们可以看下<code>before</code>方法逻辑。到现在为止我们才是万事俱备就差编写<code>单元测试</code>逻辑了，我们首先来编写访问<code>/index</code>请求路径的测试，具体测试代码如下所示：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 测试访问/index地址</span><br><span class="line"> * @throws Exception</span><br><span class="line"> */</span><br><span class="line">@Test</span><br><span class="line">public void testIndex() throws Exception &#123;</span><br><span class="line">	MvcResult mvcResult = mockMvc</span><br><span class="line">			.perform(// 1</span><br><span class="line">					MockMvcRequestBuilders.get(&quot;/index&quot;) // 2</span><br><span class="line">					.param(&quot;name&quot;,&quot;admin&quot;) // 3</span><br><span class="line">			)</span><br><span class="line">			.andReturn();// 4</span><br><span class="line"></span><br><span class="line">	int status = mvcResult.getResponse().getStatus(); // 5</span><br><span class="line">	String responseString = mvcResult.getResponse().getContentAsString(); // 6</span><br><span class="line"></span><br><span class="line">	Assert.assertEquals(&quot;请求错误&quot;, 200, status); // 7</span><br><span class="line">	Assert.assertEquals(&quot;返回结果不一致&quot;, &quot;this is index pageadmin&quot;, responseString); // 8</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>####MockMvc解析<br>我在上面代码中进行了标记，我们按照标记进行讲解，这样会更明白一些：<br><code>1</code> <code>perform</code>方法其实只是为了构建一个请求，并且返回<code>ResultActions</code>实例，该实例则是可以获取到请求的返回内容。<br><code>2</code> <code>MockMvcRequestBuilders</code>该抽象类则是可以构建多种请求方式，如：<code>Post</code>、<code>Get</code>、<code>Put</code>、<code>Delete</code>等常用的请求方式，其中参数则是我们需要请求的本项目的相对路径，<code>/</code>则是项目请求的根路径。<br><code>3</code> <code>param</code>方法用于在发送请求时携带参数，当然除了该方法还有很多其他的方法，大家可以根据实际请求情况选择调用。<br><code>4</code> <code>andReturn</code>方法则是在发送请求后需要获取放回时调用，该方法返回<code>MvcResult</code>对象，该对象可以获取到返回的视图名称、返回的Response状态、获取拦截请求的拦截器集合等。<br><code>5</code> 我们在这里就是使用到了第<code>4</code>步内的<code>MvcResult</code>对象实例获取的<code>MockHttpServletResponse</code>对象从而才得到的<code>Status</code>状态码。<br><code>6</code> 同样也是使用<code>MvcResult</code>实例获取的<code>MockHttpServletResponse</code>对象从而得到的请求返回的字符串内容。【可以查看rest返回的json数据】<br><code>7</code> 使用<code>Junit</code>内部验证类<code>Assert</code>判断返回的状态码是否正常为200<br><code>8</code> 判断返回的字符串是否与我们预计的一样。</p>
<p>###测试商品详情<br>直接上代码吧，跟上面的代码几乎一致，如下所示：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">	 * 测试查询详情</span><br><span class="line">	 * @throws Exception</span><br><span class="line">	 */</span><br><span class="line">	@Test</span><br><span class="line">	public void testDetail() throws Exception</span><br><span class="line">	&#123;</span><br><span class="line">		MvcResult mvcResult = mockMvc</span><br><span class="line">				.perform(</span><br><span class="line">						MockMvcRequestBuilders.get(&quot;/detail&quot;)</span><br><span class="line">						.param(&quot;goodId&quot;,&quot;2&quot;)</span><br><span class="line">				)</span><br><span class="line">				.andReturn(); // 5</span><br><span class="line"></span><br><span class="line">		//输出经历的拦截器</span><br><span class="line">		HandlerInterceptor[] interceptors = mvcResult.getInterceptors();</span><br><span class="line">		System.out.println(interceptors[0].getClass().getName());</span><br><span class="line"></span><br><span class="line">		int status = mvcResult.getResponse().getStatus(); // 6</span><br><span class="line">		String responseString = mvcResult.getResponse().getContentAsString(); // 7</span><br><span class="line">		System.out.println(&quot;返回内容：&quot;+responseString);</span><br><span class="line">		Assert.assertEquals(&quot;return status not equals 200&quot;, 200, status); // 8</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure></p>
<p>上面唯一一个部分需要解释下，在上面测试方法内输出了请求经历的拦截器，如果我们配置了多个拦截器这里会根据先后顺序写入到拦截器数组内，其他的<code>MockMvc</code>测试方法以及参数跟上面测试方法一致。</p>
<p>###测试添加<br>在测试类声明定义全局字段时，我们注入了<code>GoodInfoJPA</code>实例，当然单元测试也不仅仅是<code>客户端</code>也就是使用<code>MockMvc</code>方式进行的，我们也可以直接调用<code>JPA</code>、<code>Service</code>进行直接测试。下面我们来测试下商品基本信息的添加，代码如下所示：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">	 * 测试添加商品基本信息</span><br><span class="line">	 */</span><br><span class="line">	@Test</span><br><span class="line">	public void testInsert()</span><br><span class="line">	&#123;</span><br><span class="line">		/**</span><br><span class="line">		 * 商品基本信息实体</span><br><span class="line">		 */</span><br><span class="line">		GoodInfoEntity goodInfoEntity = new GoodInfoEntity();</span><br><span class="line">		goodInfoEntity.setTitle(&quot;西红柿&quot;);</span><br><span class="line">		goodInfoEntity.setOrder(2);</span><br><span class="line">		goodInfoEntity.setPrice(5.82);</span><br><span class="line">		goodInfoEntity.setTypeId(1);</span><br><span class="line">		goodInfoJPA.save(goodInfoEntity);</span><br><span class="line">		/**</span><br><span class="line">		 * 测试是否添加成功</span><br><span class="line">		 * 验证主键是否存在</span><br><span class="line">		 */</span><br><span class="line">		Assert.assertNotNull(goodInfoEntity.getTgId());</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure></p>
<p>在上面代码中并没有什么特殊的部分，是我们在使用<code>Data JPA</code>时用到的<code>save</code>方法用于执行添加，在添加完成后验证主键的值是否存在，<code>NotNull</code>时证明添加成功。</p>
<p>###测试删除<br>与添加差别不大，代码如下所示：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 测试删除商品基本信息</span><br><span class="line"> */</span><br><span class="line">@Test</span><br><span class="line">public void testDelete()</span><br><span class="line">&#123;</span><br><span class="line">	//根据主键删除</span><br><span class="line">	goodInfoJPA.delete(3);</span><br><span class="line">	</span><br><span class="line">	//验证数据库是否已经删除</span><br><span class="line">	Assert.assertNull(goodInfoJPA.findOne(3));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>在上面代码中，我们根据主键的值进行删除商品的基本信息，执行删除完成后调用selectOne方法查看数据库内是否已经不存在该条数据了。</p>
<p>#总结<br>本章主要介绍了基于<code>SpringBoot</code>平台的两种<code>单元测试</code>方式，一种是在<code>服务端</code>采用<code>Spring注入</code>方式将需要测试的<code>JPA</code>或者<code>Service</code>注入到测试类中，然后调用方法即可。另外一种则是在<code>客户端</code>采用<code>MockMvc</code>方式测试<code>Web</code>请求，根据传递的不用参数以及请求返回对象反馈信息进行验证测试。</p>
<p>本章代码已经上传到码云：<br>SpringBoot配套源码地址：<a href="https://gitee.com/hengboy/spring-boot-chapter" target="_blank" rel="noopener">https://gitee.com/hengboy/spring-boot-chapter</a><br>SpringCloud配套源码地址：<a href="https://gitee.com/hengboy/spring-cloud-chapter" target="_blank" rel="noopener">https://gitee.com/hengboy/spring-cloud-chapter</a><br>SpringBoot相关系列文章请访问：<a href="http://www.jianshu.com/p/9a08417e4e84" target="_blank" rel="noopener">目录：SpringBoot学习目录</a><br>QueryDSL相关系列文章请访问：<a href="http://www.jianshu.com/p/99a5ec5c3bd5" target="_blank" rel="noopener">QueryDSL通用查询框架学习目录</a><br>SpringDataJPA相关系列文章请访问：<a href="http://www.jianshu.com/p/615ed9c1fe84" target="_blank" rel="noopener">目录：SpringDataJPA学习目录</a><br>感谢阅读！<br>欢迎加入QQ技术交流群，共同进步。<br><img src="http://upload-images.jianshu.io/upload_images/4461954-f1d4cb64234b160a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="QQ技术交流群"></p>

        
        </div>
        <footer class="article-footer">
            <div class="share-container">


    <div class="bdsharebuttonbox">
    <a href="#" class="bds_more" data-cmd="more">分享到：</a>
    <a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间">QQ空间</a>
    <a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博">新浪微博</a>
    <a href="#" class="bds_tqq" data-cmd="tqq" title="分享到腾讯微博">腾讯微博</a>
    <a href="#" class="bds_renren" data-cmd="renren" title="分享到人人网">人人网</a>
    <a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信">微信</a>
</div>
<script>
window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{"bdSize":16}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
</script>
<style>
    .bdshare_popup_box {
        border-radius: 4px;
        border: #e1e1e1 solid 1px;
    }
    .bdshare-button-style0-16 a,
    .bdshare-button-style0-16 .bds_more {
        padding-left: 20px;
        margin: 6px 10px 6px 0;
    }
    .bdshare_dialog_list a,
    .bdshare_popup_list a,
    .bdshare_popup_bottom a {
        font-family: 'Microsoft Yahei';
    }
    .bdshare_popup_top {
        display: none;
    }
    .bdshare_popup_bottom {
        height: auto;
        padding: 5px;
    }
</style>


</div>

            
    

        </footer>
    </div>
    
        
<nav id="article-nav">
    
        <a href="/spring-boot-chapter-36/" id="article-nav-newer" class="article-nav-link-wrap">
            <strong class="article-nav-caption">上一篇</strong>
            <div class="article-nav-title">
                
                    第三十六章：基于SpringBoot架构重写SpringMVC请求参数装载
                
            </div>
        </a>
    
    
        <a href="/spring-boot-chapter-34/" id="article-nav-older" class="article-nav-link-wrap">
            <strong class="article-nav-caption">下一篇</strong>
            <div class="article-nav-title">第三十四章：SpringBoot配置类WebMvcConfigurerAdapter</div>
        </a>
    
</nav>


    
</article>


    
    

</section>
            
			
				
<aside id="sidebar">
   
        
    <div class="widget-wrap">
        <h3 class="widget-title">最新文章</h3>
        <div class="widget">
            <ul id="recent-post" class="no-thumbnail">
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/SpringBoot-核心技术/">SpringBoot 核心技术</a></p>
                            <p class="item-title"><a href="/spring-boot-chapter-52/" class="title">第五十二章：基于SpringBoot2使用Rest访问MongoDB数据</a></p>
                            <p class="item-date"><time datetime="2018-04-21T16:00:00.000Z" itemprop="datePublished">2018-04-22</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/SpringBoot-核心技术/">SpringBoot 核心技术</a></p>
                            <p class="item-title"><a href="/spring-boot-chapter-51/" class="title">第五十一章：基于SpringBoot2 &amp; MongoDB完成自动化集成</a></p>
                            <p class="item-date"><time datetime="2018-04-15T16:00:00.000Z" itemprop="datePublished">2018-04-16</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/SpringBoot-核心技术/">SpringBoot 核心技术</a></p>
                            <p class="item-title"><a href="/spring-boot-chapter-50/" class="title">第五十章：SpringBoot2.0新特性 - 岂止至今最简单redis缓存集成</a></p>
                            <p class="item-date"><time datetime="2018-04-14T16:00:00.000Z" itemprop="datePublished">2018-04-15</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/SpringBoot-核心技术/">SpringBoot 核心技术</a></p>
                            <p class="item-title"><a href="/spring-boot-chapter-49/" class="title">第四十九章：SpringBoot2.0新特性 - 你get到WebMvcConfigurer两种配置方式了吗？</a></p>
                            <p class="item-date"><time datetime="2018-03-16T16:00:00.000Z" itemprop="datePublished">2018-03-17</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/SpringBoot-核心技术/">SpringBoot 核心技术</a></p>
                            <p class="item-title"><a href="/spring-boot-chapter-48/" class="title">第四十八章：SpringBoot2.0新特性 - RabbitMQ信任package设置</a></p>
                            <p class="item-date"><time datetime="2018-03-12T16:00:00.000Z" itemprop="datePublished">2018-03-13</time></p>
                        </div>
                    </li>
                
            </ul>
        </div>
    </div>

    
        
    <div class="widget-wrap">
        <h3 class="widget-title">分类</h3>
        <div class="widget">
            <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/QueryDSL-核心技术/">QueryDSL 核心技术</a><span class="category-list-count">7</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/SpringBoot-核心技术/">SpringBoot 核心技术</a><span class="category-list-count">36</span></li></ul>
        </div>
    </div>

    
        
    <div class="widget-wrap">
        <h3 class="widget-title">归档</h3>
        <div class="widget">
            <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/04/">四月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">三月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/01/">一月 2018</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/12/">十二月 2017</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/11/">十一月 2017</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/10/">十月 2017</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/09/">九月 2017</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/08/">八月 2017</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/07/">七月 2017</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/06/">六月 2017</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/05/">五月 2017</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/04/">四月 2017</a><span class="archive-list-count">5</span></li></ul>
        </div>
    </div>

    
        
    <div class="widget-wrap">
        <h3 class="widget-title">标签</h3>
        <div class="widget">
            <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/QueryDSL/">QueryDSL</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/SpringBoot/">SpringBoot</a><span class="tag-list-count">43</span></li></ul>
        </div>
    </div>

    
        
    <div class="widget-wrap">
        <h3 class="widget-title">标签云</h3>
        <div class="widget tagcloud">
            <a href="/tags/QueryDSL/" style="font-size: 10px;">QueryDSL</a> <a href="/tags/SpringBoot/" style="font-size: 20px;">SpringBoot</a>
        </div>
    </div>

    
        
    <div class="widget-wrap widget-list">
        <h3 class="widget-title">链接</h3>
        <div class="widget">
            <ul>
                
                    <li>
     				<a href="http://hexo.io" target="_blank">Hexo</a>
					</li>
                
            </ul>
        </div>
    </div>


    
    <div id="toTop" class="fa fa-angle-up"></div>
</aside>

				
        </div>
        <!--引入不蒜子-->
<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<footer id="footer">
    <div class="outer">
        <div id="footer-info" class="inner">
            <span  id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
            &copy; 2017 - 2018 恒宇少年 - 版权所有<br>
        </div>
    </div>
</footer>

        


    
        <script src="/libs/lightgallery/js/lightgallery.min.js"></script>
        <script src="/libs/lightgallery/js/lg-thumbnail.min.js"></script>
        <script src="/libs/lightgallery/js/lg-pager.min.js"></script>
        <script src="/libs/lightgallery/js/lg-autoplay.min.js"></script>
        <script src="/libs/lightgallery/js/lg-fullscreen.min.js"></script>
        <script src="/libs/lightgallery/js/lg-zoom.min.js"></script>
        <script src="/libs/lightgallery/js/lg-hash.min.js"></script>
        <script src="/libs/lightgallery/js/lg-share.min.js"></script>
        <script src="/libs/lightgallery/js/lg-video.min.js"></script>
    
    
        <script src="/libs/justified-gallery/jquery.justifiedGallery.min.js"></script>
    
    



<!-- Custom Scripts -->
<script src="/js/main.js"></script>

    </div>
</body>
</html>